home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / futils / futils~1 / src / shell13s.zoo / shell1.3 / lib / putenv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-06  |  2.4 KB  |  94 lines

  1. /* Copyright (C) 1991 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <errno.h>
  20. #ifdef STDC_HEADERS
  21. #include <stdlib.h>
  22. #else
  23. extern int errno;
  24. #include <sys/types.h>
  25. #endif
  26. #if defined(STDC_HEADERS) || defined(USG)
  27. #include <string.h>
  28. #define index strchr
  29. #define bcopy(s, d, n) memcpy((d), (s), (n))
  30. #else
  31. #include <strings.h>
  32. #endif
  33. #ifdef POSIX
  34. #include <unistd.h>
  35. #endif
  36. #ifndef NULL
  37. #define NULL 0
  38. #endif
  39. extern char **environ;
  40.  
  41. /* Put STRING, which is of the form "NAME=VALUE", in the environment.  */
  42. int
  43. putenv (string)
  44.      char *string;
  45. {
  46.   char *name_end = index (string, '=');
  47.   register size_t size;
  48.   register char **ep;
  49.  
  50.   if (name_end == NULL)
  51.     {
  52.       /* Remove the variable from the environment.  */
  53.       size = strlen (string);
  54.       for (ep = environ; *ep != NULL; ++ep)
  55.     if (!strncmp (*ep, string, size) && (*ep)[size] == '=')
  56.       {
  57.         while (ep[1] != NULL)
  58.           {
  59.         ep[0] = ep[1];
  60.         ++ep;
  61.           }
  62.         *ep = NULL;
  63.         return 0;
  64.       }
  65.     }
  66.  
  67.   size = 0;
  68.   for (ep = environ; *ep != NULL; ++ep)
  69.     if (!strncmp (*ep, string, name_end - string) &&
  70.     (*ep)[name_end - string] == '=')
  71.       break;
  72.     else
  73.       ++size;
  74.  
  75.   if (*ep == NULL)
  76.     {
  77.       static char **last_environ = NULL;
  78.       char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
  79.       if (new_environ == NULL)
  80.     return -1;
  81.       (void) bcopy ((char *) environ, (char *) new_environ, size * sizeof (char *));
  82.       new_environ[size] = (char *) string;
  83.       new_environ[size + 1] = NULL;
  84.       if (last_environ != NULL)
  85.     free ((char *) last_environ);
  86.       last_environ = new_environ;
  87.       environ = new_environ;
  88.     }
  89.   else
  90.     *ep = (char *) string;
  91.  
  92.   return 0;
  93. }
  94.